package com.party.mobile.web.controller.subject;

import com.google.common.base.Strings;
import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.subject.Subject;
import com.party.core.model.system.TargetType;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.subject.SubjectBizService;
import com.party.mobile.web.annotation.VisitCollect;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import com.party.mobile.web.dto.subject.output.SubjectOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 专题控制层
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/subject/subject")
public class SubjectController {

	@Autowired
	private SubjectBizService subjectBizService;

	@Autowired
	private CurrentUserBizService currentUserBizService;

	Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 获取专题详情
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDetails")
	@VisitCollect(module = TargetType.ARTICLE)
	public AjaxResult getDetails(String id, HttpServletRequest request) {
		if (Strings.isNullOrEmpty(id)) {
			return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数不合法");
		}

		try {
			SubjectOutput output = subjectBizService.getDetails(id, request);
			return AjaxResult.success(output);
		} catch (BusinessException be) {
			return AjaxResult.error(be.getCode(), be.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping(value = "/list")
	public AjaxResult getDetails(Subject subject,Page page) {
		List<SubjectOutput> outputs = subjectBizService.getList(subject,page);
		return AjaxResult.success(outputs, page);
	}


	/**
	 * 获取我参加的众筹的机构的手机官网
	 *
	 * @param request 当前用户
	 * @param page    分页
	 * @return 手机官网数据
	 */
	@ResponseBody
	@Authorization
	@RequestMapping("getMySubjectList")
	public AjaxResult getMySubject(HttpServletRequest request, Page page) {
		try {
			CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
			List<SubjectOutput> subjectOutputs = subjectBizService.getMySubject(currentUser.getId(), page);
			return AjaxResult.success(subjectOutputs, page);
		} catch (Exception e) {
			logger.error("获取我参加的众筹的机构的手机官网异常", e);
			return AjaxResult.error(100, "获取我的手机官网异常");
		}
	}
}
